package com.gzdh.dishrecognition.ui.fragment;

import static android.content.Context.MODE_PRIVATE;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.os.storage.StorageManager;
import android.text.format.Formatter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.dhkj.web.basic.Constans;
import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.ui.view.MyCircleProgress;
import com.gzdh.dishrecognition.util.SystemUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.lang.reflect.Method;
import java.math.BigDecimal;

public class SysInfoFragment extends Fragment {


    private Context mContext;

    private Activity activity;

    public SysInfoFragment(Activity activity) {
        this.activity = activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment_sys_info, container, false);
        mContext = view.getContext();
        SharedPreferences sysInfo = mContext.getSharedPreferences("sysCost", MODE_PRIVATE);
        String sysApi = sysInfo.getString("api", null);//读取username
        TextView deviceNameTv = view.findViewById(R.id.deviceNameTv);
        TextView snTv = view.findViewById(R.id.snTv);
        TextView ramTv = view.findViewById(R.id.ramTv);
        TextView romTv = view.findViewById(R.id.romTv);
        TextView sysCenterUrlTv = view.findViewById(R.id.sysCenterUrlTv);
        TextView apiUrlTv = view.findViewById(R.id.apiUrlTv);
        TextView sn2Tv = view.findViewById(R.id.sn2Tv);
        MyCircleProgress ramProgress = view.findViewById(R.id.ramProgress);
        MyCircleProgress romProgress = view.findViewById(R.id.romProgress);
        deviceNameTv.setText(SystemUtils.getModel() + "正在运行");
        snTv.setText("SN: " + SystemUtils.getSerialNumber());
        sn2Tv.setText( SystemUtils.getSerialNumber());


        int ramTemp = SystemUtils.getTotalRam();
        ramTv.setText(ramTemp + "GB");

        ActivityManager mActivityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        //获得MemoryInfo对象
        ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
        //获得系统可用内存，保存在MemoryInfo对象上
        mActivityManager.getMemoryInfo(memoryInfo);
        long memSize = memoryInfo.availMem;

        int ramMem1 = ramTemp * 1024;
        int ramMem2 = (int) memSize / (1024 * 1024);
        ramProgress.setMax(ramMem1);
        ramProgress.setCurrent(ramMem1 - ramMem2);
        sysCenterUrlTv.setText(Constans.SYS_API_URL);
        apiUrlTv.setText(sysApi);


        //获取一个磁盘的状态对象
        StatFs stat = new StatFs(Environment.getRootDirectory().getPath());
        //获得一个扇区的大小
        long blockSizes = stat.getBlockSizeLong();
        //获取扇区总数
        long totalBlocks = stat.getBlockCountLong();
        //获得可用扇区数量
        long availableBlocks = stat.getAvailableBlocksLong();

        BigDecimal zh = new BigDecimal(1024);
        BigDecimal memory = (new BigDecimal(blockSizes).multiply(new BigDecimal(totalBlocks))).divide(zh).divide(zh);
        BigDecimal availableMemory = (new BigDecimal(blockSizes).multiply(new BigDecimal(availableBlocks))).divide(zh).divide(zh);
        romTv.setText(memory.divide(zh).setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "GB");
        romProgress.setCurrent(memory.subtract(availableMemory).intValue());
        romProgress.setMax(memory.intValue());
        return view;
    }


}